
function executeScriptInClient(command) {
    var codediv = document.createElement("button");
    codediv.setAttribute("style", "display:hidden");
    codediv.setAttribute("onclick", command);
    document.body.appendChild(codediv);
    codediv.click();
    document.body.removeChild(codediv);
}


function doActions(request, actions) {
    if (!actions.replace) 
        return;

    var obj = request.obj;
    var new_obj = request.new_obj;
    obj.parentNode.replaceChild(new_obj, obj);

    if (actions.checkForm) {
        var parent = new_obj.parentNode;
        while (parent.nodeType == 1) {
            if (parent.nodeName.toLowerCase() == "form") {
                 command = "document.all." + parent.name + "." + new_obj.id +
                     " = document.all." + new_obj.id + ";";
                 executeScriptInClient(command);
            }
            parent = parent.parentNode;
        }
    }
}

function doObjectActions(requestParameters, obj) {
obj.command = "ActionList";
chrome.extension.sendRequest(obj, function(response) 
{doActions(requestParameters, response);
});
}

function executeScriptInClient(command) {
    var codediv = document.createElement("button");
    codediv.setAttribute("style", "display:hidden");
    codediv.setAttribute("onclick", command);
    document.body.appendChild(codediv);
    codediv.click();
    document.body.removeChild(codediv);
}

function replaceDocument() {
    var s = document.getElementsByTagName("object");
    console.log(s.length);
    for (var i = 0; i < s.length; ++i) {
        var obj = s[i];
        var new_obj = document.createElement("object");
        console.log("Found object " + i + ", id = " + obj.id);
        // Traverse through attributes
        for (var attri = 0; attri < obj.attributes.length; ++attri) {
            var attr = obj.attributes[attri];
            if (attr.name == "classid") {
                var clsid = attr.value;
                var compos = clsid.indexOf(":");
                if (clsid.substring(0, compos).toLowerCase() != "clsid") break;
                clsid = clsid.substring(compos + 1);
                new_obj.setAttribute("clsid", "{" + clsid + "}");

            } else if (attr.name.substr(0, 2) == "on") {
                new_obj.setAttribute("" + attr.name, attr.value);
            } else if (attr.name == "codebase") {
                var codebase = attr.value;
                var reg = new RegExp("^[a-z]+://.*");
                if (reg.exec(attr.value) == null)
                    codebase = location.origin + "/" + codebase;
                new_obj.setAttribute("codeBaseUrl", codebase);
            } else {
                new_obj.setAttribute(attr.name, attr.value);
            }

        } // for attrubites
        // Traverse through subelements
        for (var j = 0; j < obj.childNodes.length; ++j) {
            var elem = obj.childNodes[j];
            if (elem.nodeName.toLowerCase() == "param") {
                new_obj.setAttribute("param_" + elem.getAttribute("name"), elem.getAttribute("value"));
            } else if (elem.nodeName.toLowerCase() == "object" || elem.nodeName.toLowerCase() == "embed") {
                // nested object, should support npapi already
                new_obj.removeAttribute("clsid");
            }
        } // for elements
        if (!new_obj.hasAttribute("clsid")) {
            continue;
        }
        new_obj.setAttribute("type", "application/x-itst-activex");
        doObjectActions({obj:obj, new_obj:new_obj}, {url: location.href, clsid: new_obj.getAttribute("clsid")});

    } // for objects
};
